//获取应用实例
let utils = require("../../utils/utils.js");
var app = getApp()



Page({
	data: {
		/* myglass-- */
		typeindex: 1,
		getOrderUrl: '/order/query',
		cancelcancel: '/order/cancel',
		checkOyoUrl: '/oyo/checkOyo',
		nonOrder: false
			/*上拉加载参数*/
			,
		orderLists: [],
		isLoading: false,
		finished: false,
		isUpLoading: false,
		pageSize: 10 // 每页条数
			,
		offset: 0 // 页码
			,
		limit: 10,
		bottom: false,
		orderAccount: 0,
		payAccount: 0,
		footers: [{
				icon: 'icon-tianchongxing-',
				name: '首页'
			},
			{
				icon: 'icon-dingdan1',
				name: '订单'
			},
			{
				icon: 'icon-wode1',
				name: '我的'
			}
		],
		orderlistnum:[]
	},
	goconfirmorder(e) {
		let id = e.currentTarget.dataset.type;
		if (id == 1) {
			wx.navigateTo({
				'url': '../confirmorder/confirmorder'
			})
		}

	},
	// 加载
	onLoad(options) {
		let that = this;
		let type = options.type;
		let offset = that.data.offset = 0;
		that.data.type = type;
		//that.typeorder(type, offset) /* 加载订单数据 type为订单类型 num为页数*/
	},
	// 显示
	onShow() {
		this.getOrderData();
	},
	getOrderData() {
		let that = this;
		let indexData=wx.getStorageSync('indexData');
		let userData=indexData.user;
		let _data = {
			url: that.data.getOrderUrl,
			methods:'post',
			nonelist:true,
			data: {'userId':userData.userId,'type':userData.type,'deviceId':userData.deviceCode,methods:'post',nonelist:true,offset:this.data.offset}
		};
		utils.request.call(_data, (res) => {
			console.log(res.data.data.content);
			this.data.orderlistnum=res.data.data.content;
			let orderLists = res.data.data.content;
			orderLists.map((val, key) => {
				console.log(val);
				val.status = false;
				val.createTime = utils.date(val.createTime);
				val.orderAccount = 0;
				val.payAccount = 0;
				val.items.map((valone, index) => {
					valone.itemImages=app.globalData.baseurl+valone.itemImages;
					valone.reTime = utils.date(valone.reTime);
					val.orderCancelGlassStatus = true;
					valone.payAmount=Number(valone.payAmount).toFixed(2);
					if (valone.glassesStatus == 1) {
						val.orderGlassStatus = true;
					} else {
						val.orderCancelGlassStatus = false;
					};
				});

			});
			console.log(orderLists);
			
			if (orderLists.length >= 0) {
				const content = orderLists;
				if (content.length < that.data.pageSize) {
					// 最后一页不足10条的情况
					if (that.data.offset > 0) {
						console.log(0);
						that.data.bottom = true;
						that.setData({
							bottom: true
						});
					};
					
					//return false;
				};
				// 处理数据
				if (that.data.offset === 0) {
					that.data.orderLists = content;
					console.log(that.data.orderLists);
					that.setData({
						orderLists:that.data.orderLists,
					});
					if (orderLists.length == 0 || !orderLists) {
						let nonOrder = true;
						that.setData({
							nonOrder,
						})
					};
					//return false;
				} else {
					that.data.orderLists = that.data.orderLists.concat(content);
					that.setData({
						orderLists:that.data.orderLists,
					});
					if (orderLists.length == 0 || !orderLists) {
						let bottom = true;
						that.setData({
							bottom,
						})
					};
					return false;
				};
			};
		});
	},
	downPull: function(e) {
		let index=e.currentTarget.dataset.index;
		this.data.orderLists[index].status = !this.data.orderLists[index].status;
		this.setData({
			orderLists:this.data.orderLists
		})
	},
	cancelOrder: function(e) {
		let index=e.currentTarget.dataset.index;
		console.log(index);
		let that = this;
		utils.confirm('确认要取消订单吗？', () => {
			console.log(index);
			let orderNumber = that.data.orderLists[index].orderNumber;
			let _data = {
				url: that.data.cancelcancel,
				methods:'post',
				nonelist:true,
				data: {'orderNumber':orderNumber}
			};
			utils.request.call(_data, (res) => {
				if(res.data.status==200){
					utils.tips('您已成功取消订单！');
					setTimeout(function() {
						that.getOrderData();
					}, 1000);
				}else{
					utils.tips(res.data.msg);
				};
			});
		});
	},
	/*点击取镜*/
	fetchCode: function(e) {
		let index=e.currentTarget.dataset.index;
		let that = this;
		let orderNumber = that.data.orderLists[index].orderNumber;
		let _data = {
			url: that.data.checkOyoUrl,
			methods:'post',
			nonelist:true,
			data: {'orderNumber':orderNumber}
		};
		utils.request.call(_data, (res) => {
			if (res.data.status == 200) {
				wx.setStorageSync('fetchCodeInfo',res.data.data);
				wx.redirectTo({
					url:'../fetchcode/fetchcode'
				});
			};
		});
	},
	/* 导航页跳转*/
	pageLinks: function(e) {
		let index = e.currentTarget.dataset.index;
		console.log(0);
		if (index == 0) {
			wx.reLaunch({
				url: '../index/index'
			});
		} else if (index == 1) {
			return false;
		} else {
			wx.reLaunch({
				url: '../mine/mine'
			});
		};
	},
	// 下拉刷新

	onPullDownRefresh() {
		///this.offset++;
		
	},

	/**
	 * 用户点击右上角分享
	 */
	onShareAppMessage: function() {
		let sharepages = app.globalData.sharepage
		return sharepages;
	},
	/* 微信上拉加载更多 */
	onReachBottom() {
		let that = this;
		let type = that.data.type;
		let offset = that.data.offset;
		let orderlistnum = that.data.orderlistnum;
		if (offset >= 0 && orderlistnum.length < 10) {
			console.log(0);
			that.setData({
				bottom: true
			});
			return false;
		}else {
			that.setData({
				loading: true
			})
			offset++;
			this.data.offset=offset;
			this.getOrderData(); /* 加载订单数据  type为付款类型 num为页数*/
		}
	},



})
